package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.JsonUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ActorAdapter;
import flc.ast.bean.MyActorBean;
import flc.ast.bean.MyCollectBean;
import flc.ast.databinding.ActivityDetailBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.IntentUtil;
import stark.common.bean.StkResBean;

public class DetailActivity extends BaseAc<ActivityDetailBinding> {

    public static StkResBean detail;
    private ActorAdapter actorAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_detail;
    }

    @Override
    protected void initView() {
        mDataBinding.ivDetailBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivDetailCollect.setOnClickListener(this);
        mDataBinding.ivShare.setOnClickListener(this);
        mDataBinding.rvActorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        actorAdapter = new ActorAdapter();
        mDataBinding.rvActorList.setAdapter(actorAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivDetailCollect:
                List<MyCollectBean> listSp = AppSpUtils.getCollectList();
                if (detail.isSelected()) {
                    for (MyCollectBean myCollectBean : listSp) {
                        if (myCollectBean.getPath().equals(detail.getThumbUrl())) {
                            listSp.remove(myCollectBean);
                            break;
                        }
                    }
                    AppSpUtils.saveCollectList(listSp);
                    detail.setSelected(false);
                } else {
                    List<MyCollectBean> listSave = new ArrayList<>();
                    try {
                        Gson gson = new Gson();
                        String json = gson.toJson(detail.getExtra());
                        JSONObject jsonObject = new JSONObject(json);
                        String releaseDate = jsonObject.getString("releaseDate");
                        int index = releaseDate.indexOf("(");
                        String dateStr = releaseDate.substring(0, index);
                        Date date = TimeUtils.string2Date(dateStr, new SimpleDateFormat("yyyy-MM-dd"));
                        listSave.add(new MyCollectBean(detail.getName(), detail.getThumbUrl(), date.getTime()));
                        if (listSp != null && listSp.size() != 0) {
                            listSp.addAll(listSave);
                            AppSpUtils.saveCollectList(listSp);
                        } else {
                            AppSpUtils.saveCollectList(listSave);
                        }
                        detail.setSelected(true);
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                }
                getSelect();
                break;
            case R.id.ivShare:
                IntentUtil.shareText(mContext, detail.getUrl());
                break;
        }
    }

    @Override
    protected void initData() {
        if (detail == null) return;
        Glide.with(mContext).load(detail.getThumbUrl()).into(mDataBinding.ivDetailImg);
        mDataBinding.tvDetailName.setText(detail.getName());
        mDataBinding.tvDetailDesc.setText(detail.getDesc());
        getSelect();
        mDataBinding.tvDetailActor.setText(getString(R.string.actor) + detail.getActor());
        try {
            Gson gson = new Gson();
            String json = gson.toJson(detail.getExtra());
            JSONObject jsonObject = new JSONObject(json);
            String releaseDate = jsonObject.getString("releaseDate");
            int index = releaseDate.indexOf("(");
            String dateStr = releaseDate.substring(0, index);
            Date date = TimeUtils.string2Date(dateStr, new SimpleDateFormat("yyyy-MM-dd"));
            if (date != null) {
                mDataBinding.tvDetailDate.setText(getString(R.string.release_date) + TimeUtils.date2String(date, new SimpleDateFormat("yyyy年MM月dd日")));
            } else {
                mDataBinding.tvDetailDate.setText(getString(R.string.release_date) + dateStr);
            }
            String actorList = jsonObject.getString("actorList");
            List<MyActorBean> listActor = new Gson().fromJson(actorList, new TypeToken<List<MyActorBean>>() {
            }.getType());
            actorAdapter.setList(listActor);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    private void getSelect() {
        if (detail.isSelected()) {
            mDataBinding.ivDetailCollect.setImageResource(R.drawable.ysc1);
        } else {
            mDataBinding.ivDetailCollect.setImageResource(R.drawable.wsc1);
        }
    }

}